package com.cloudcc.frame.comclass;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.core.UserInfo;
import net.sf.json.JSONObject;

import java.util.List;

public class CpeleoppTest {
    private static CCService cs;

    private UserInfo userInfo;

    public CpeleoppTest(UserInfo userInfo) throws Exception {
        cs = new CCService(userInfo);
        this.userInfo = userInfo;
    }

    public JSONObject cp(String id) {
        String userId = userInfo.getUserId();
        String status1 = "跟进中";
        String jieduan = "1-发现机会";
        String findEleoppById = "select * from eleopp where id ='" + id + "' ";
        List<CCObject> eleoppInfo = cs.cqlQuery("eleopp", findEleoppById);
        String TYPE = "未完结";
        JSONObject jsonObject = new JSONObject();
        if (eleoppInfo.size() > 0) {
            String psmc = eleoppInfo.get(0).get("fk_psmc") == null ? "" : eleoppInfo.get(0).get("fk_psmc") + "";
            String findAccountPsmcSql = "select id from ghjl where yh ='" + userId + "' and jyzx ='" + psmc + "' and is_deleted ='0' ";
            List<CCObject> ghjlInfo = cs.cqlQuery("ghjl", findAccountPsmcSql);
            if (ghjlInfo.size() == 0) {
                /*提示 您暂无权限对该客户创建商机*/
                jsonObject.put("action", "alert");
                jsonObject.put("message", "您暂无权限对该客户创建商机");
                return jsonObject;
            }
            String status = ghjlInfo.get(0).get("status1") == null ? "" : ghjlInfo.get(0).get("status1") + "";
            if (status.equals(TYPE)) {
                /*提示 当前商机未完结，如需复制商机请先将当前商机进行丢单*/
                jsonObject.put("action", "alert");
                jsonObject.put("message", "当前商机未完结，如需复制商机请先将当前商机进行丢单");
                return jsonObject;
            }
            CCObject eleopp = new CCObject("eleopp");
            eleopp.put("name", eleoppInfo.get(0).get("name"));
            eleopp.put("fk_psmc", eleoppInfo.get(0).get("fk_psmc"));
            eleopp.put("jieduan", jieduan);
            eleopp.put("frequence", eleoppInfo.get(0).get("frequence"));
            eleopp.put("province1", eleoppInfo.get(0).get("province1"));
            eleopp.put("contact_address", eleoppInfo.get(0).get("contact_address"));
            eleopp.put("fk_city_name1", eleoppInfo.get(0).get("fk_city_name1"));
            eleopp.put("fk_country_name1", eleoppInfo.get(0).get("fk_country_name1"));
            eleopp.put("intention", eleoppInfo.get(0).get("intention"));
            eleopp.put("pic_info", eleoppInfo.get(0).get("pic_info"));
            eleopp.put("one_year_num", eleoppInfo.get(0).get("one_year_num"));
            eleopp.put("habbit", eleoppInfo.get(0).get("habbit"));
            eleopp.put("strategy", eleoppInfo.get(0).get("strategy"));
            eleopp.put("pay_method", eleoppInfo.get(0).get("pay_method"));
            eleopp.put("decision", eleoppInfo.get(0).get("decision"));
            eleopp.put("tender", eleoppInfo.get(0).get("tender"));
            eleopp.put("other_info", eleoppInfo.get(0).get("other_info"));
            eleopp.put("source", eleoppInfo.get(0).get("source"));
            eleopp.put("accountTime", eleoppInfo.get(0).get("accountTime"));
            eleopp.put("leader", eleoppInfo.get(0).get("leader"));
            eleopp.put("createbyid", eleoppInfo.get(0).get("createbyid"));
            eleopp.put("ownerid", userId);
            eleopp.put("lastmodifybyid", eleoppInfo.get(0).get("lastmodifybyid"));
            eleopp.put("khmc", eleoppInfo.get(0).get("khmc"));
            eleopp.put("recordtype", eleoppInfo.get(0).get("recordtype"));
            eleopp.put("status1", status1);
            ServiceResult insert = cs.insert(eleopp);
            String eleoppId = insert.get("id") == null ? "" : insert.get("id") + "";
            jsonObject.put("status","000");
            jsonObject.put("action", "redirect");
            jsonObject.put("mode","now");
            jsonObject.put("url", "/#/commonObjects/detail/"+eleoppId+"/DETAIL");

        }
        return jsonObject;
    }
}
